/*
 * This file is part of the Boring Engine.
 *
 * Copyright (c) 2011 by pf5234 <pf5234@users.sourceforge.net>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.googlecode.boringengine.internal.openglrendering;

import com.sun.opengl.util.Animator;
import com.sun.opengl.util.FPSAnimator;
import javax.media.opengl.DebugGL;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import com.googlecode.boringengine.Log;
import com.googlecode.boringengine.Main;
import com.googlecode.boringengine.internal.Engine;

public class OpenGLCanvas implements GLEventListener {

    private Animator animator;
    private GLCanvas c;

    public OpenGLCanvas() {
        GLCapabilities glCaps = new GLCapabilities();
        /*glCaps.setRedBits(8);
        glCaps.setBlueBits(8);
        glCaps.setGreenBits(8);
        glCaps.setAlphaBits(8);*/

        GLCanvas canvas = new GLCanvas(glCaps);

        canvas.addGLEventListener(this);
        c = canvas;

        int fps = 60;
        if (System.getProperty("os.name").contains("Windows"))
            fps *= 2;
        animator = new FPSAnimator(canvas, fps);
        animator.start();
        //c = canvas;
    }

    public void init(GLAutoDrawable drawable) {
        try {
            if (Main.DEBUG)
                drawable.setGL(new DebugGL(drawable.getGL()));
            GL gl = drawable.getGL();
            logGLInfo(gl);
            gl.glEnable(gl.GL_BLEND);
            gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA);
            gl.glDisable(gl.GL_DEPTH_TEST);
            gl.glClearColor(1, 1, 1, 1);
            //MainLoop.initSprites();
        } catch (Exception e) {
            Log.fatalException(e, "");
        }
    }

    public void display(GLAutoDrawable drawable) {
        Engine.uploadSprites();
        Engine.draw();
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        GL gl = drawable.getGL();
        gl.glMatrixMode(gl.GL_PROJECTION);
        gl.glLoadIdentity();

        gl.glOrtho(0, width, height, 0, -1, 1);

        gl.glMatrixMode(gl.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(0, 0, 0);
    }

    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {

    }

    public static boolean canCreateCanvas() {
        try {
            GLCanvas tmp = new GLCanvas();
            return true;
        } catch (Throwable ex) {
            Log.info("Throwable: %s", ex);
            return false;
        }
    }

    public GLCanvas getCanvas() {
        return c;
    }
    
    private void logGLInfo(GL gl) {
        Log.info("OpenGL version: %s", gl.glGetString(GL.GL_VERSION));
        Log.info("OpenGL vendor: %s", gl.glGetString(GL.GL_VENDOR));
        Log.info("OpenGL renderer: %s", gl.glGetString(GL.GL_RENDERER));
    }
}
